<?php


namespace IDock\Insurance\QHIC;

use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\QHIC\Request\EPolicyDownloadRequest;
use IDock\Insurance\QHIC\Request\NonCarProductListRequest;
use IDock\Insurance\QHIC\Request\PolicyPaymentRequest;
use IDock\Insurance\QHIC\Request\PremiumInquiryRequest;
use IDock\Insurance\QHIC\Request\ProposalRecallRequest;
use IDock\Insurance\QHIC\Request\ProposalStatusRequest;
use IDock\Insurance\QHIC\Request\UnderwritingRecallRequest;
use IDock\Insurance\QHIC\Request\UnderwritingRequest;
use IDock\Insurance\QHIC\Request\UploadFileRequest;
use IDock\Insurance\QHIC\Request\ValidateCarRequest;
use IDock\Insurance\QHIC\Request\VehicleActualValueRequest;
use IDock\Insurance\QHIC\Request\VehicleModelRequest;
use IDock\Insurance\QHIC\Response\EPolicyDownloadResponse;
use IDock\Insurance\QHIC\Response\NonCarProductListResponse;
use IDock\Insurance\QHIC\Response\PolicyPaymentResponse;
use IDock\Insurance\QHIC\Response\PremiumInquiryResponse;
use IDock\Insurance\QHIC\Response\ProposalRecallResponse;
use IDock\Insurance\QHIC\Response\ProposalStatusResponse;
use IDock\Insurance\QHIC\Response\UnderwritingRecallResponse;
use IDock\Insurance\QHIC\Response\UnderwritingResponse;
use IDock\Insurance\QHIC\Response\UploadFileResponse;
use IDock\Insurance\QHIC\Response\ValidateCarResponse;
use IDock\Insurance\QHIC\Response\VehicleActualValueResponse;
use IDock\Insurance\QHIC\Response\VehicleModelResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\PremiumUnderwritingPrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

class QHICConstruct extends Builder implements IDockInterface
{
    public $config = [];

    public function __construct($config)
    {
        $this->config = $config;

        $this->CDataSource = $config['CDataSource'] ?? '';
        $this->CBrkrCde = $config['CBrkrCde'] ?? '';
        $this->secret = $config['secret'] ?? '';
        $this->notifyUrl = $config['notifyUrl'] ?? '';
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor)
    {
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): Response
    {
        $request = new VehicleActualValueRequest();
        $request->setVisitor($vehicleActualValueVisitor);

        /*** @var VehicleActualValueResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 询价前置
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return PremiumInquiryResponse
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): PremiumInquiryResponse
    {
        $request = new PremiumInquiryRequest;
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var PremiumInquiryResponse $response */
        $response = $this->execute($request);

        //非车
        $responseNonCar = $response->printer->nonCarInquiryPrinter;
        if ($responseNonCar) {
            $responseNonCarList = $responseNonCar->getNonCarInquirySingle();
            $responseNonCarList = array_combine(array_column($responseNonCar->getNonCarInquirySingle(), 'argc'), $responseNonCarList);


            $nonCarInquiryPrinter = new NonCarInquiryPrinter();
            $totalPremium = 0;
            foreach ($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList() as $item) {
                /*** @var $item NonCarProductVisitor */
                $single = new NonCarInquirySinglePrinter();
                $single->premium = $item->premium;
                $single->planCode = $item->planCode;
                $single->productCode = $item->productCode;
                $single->statusCode = 'success';
                $totalPremium += $single->premium * $item->insuredNum;

                /*** @var $sub NonCarSubCoveragePrinter */
                foreach ($item->subCoverage as $sub) {
                    $single->argc[$sub->subCode] = $sub->subCode . '--' . $sub->premium;
                }
                ksort($single->argc);
                $single->argc = implode(',', $single->argc);

                if (key_exists($single->argc, $responseNonCarList)) {
                    $single->inquiryNo = $responseNonCarList[$single->argc]->inquiryNo;
                    $single->argc = [];
                    $single->argc['inquiryNo'] = $single->inquiryNo;
                }

                $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
            }
            $nonCarInquiryPrinter->totalPremium = $totalPremium;
            $response->printer->nonCarInquiryPrinter = $nonCarInquiryPrinter;
        }

        if ($response->isSuccess() && $response->printer->success) {
            //存一下保费，支付的时候用
            /*** @var PremiumInquiryPrinter $printer */
            $printer = $response->printer;
            $response->printer->argc['biPrice'] = $printer->biPremiumPrinter->biPremium;
            $response->printer->argc['ciPrice'] = round(floatval($printer->ciPremiumPrinter->ciPremium) + floatval($printer->carTaxPrinter->carShipTax), 2);
            if ($premiumInquiryVisitor->getVehicleVisitor()->isNewEnergy) {
                //新能源险种代码转换
                $response->newEnergy();
            }

        }
        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return Response
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        //非车核保
        //$this->nonCarUnderwriting($premiumInquiryVisitor->getNonCarInquiryPrinter());

        /*** @var UnderwritingResponse $response */
        $underwritingResponse = $this->execute($request);
        if ($underwritingResponse->isSuccess()) {
            /*** @var PremiumUnderwritingPrinter $printer */
            $printer = $underwritingResponse->printer;
            $printer->biInquiryNo = $premiumInquiryVisitor->getUnderwritingVisitor()->biInquiryNo;
            $printer->ciInquiryNo = $premiumInquiryVisitor->getUnderwritingVisitor()->ciInquiryNo;
            $underwritingResponse->printer = $printer;
        }
        return $underwritingResponse;
    }

    /**
     * 支付
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): PolicyPaymentResponse
    {
        $request = new PolicyPaymentRequest();
        $request->setVisitor($paymentVisitor);

        /*** @var PolicyPaymentResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return Response
     */
    public function uploadFile(UploadFileVisitor $uploadFileVisitor): Response
    {
        $request = new UploadFileRequest();
        $request->setVisitor($uploadFileVisitor);

        /*** @var UploadFileResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 投保单状态查询
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return ProposalStatusResponse
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): ProposalStatusResponse
    {
        $request = new ProposalStatusRequest();
        $request->setVisitor($proposalStatusVisitor);

        /*** @var ProposalStatusResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 人工核保撤单接口
     * @param ProposalRecallVisitor $revokeVisitor
     * @return Response
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): Response
    {
        $request = new ProposalRecallRequest();
        $request->setVisitor($revokeVisitor);

        /*** @var ProposalRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return Response
     */
    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): Response
    {
        $request = new EPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var EPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 核保撤回
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return mixed|void
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): Response
    {
        $request = new UnderwritingRecallRequest();
        $request->setVisitor($recallVisitor);

        /*** @var UnderwritingRecallResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 保单详情
     * @param PolicyInfoVisitor $policyInfoVisitor
     * @return Response
     */
    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): Response
    {
        $request = new NonCarProductListRequest();
        $request->setVisitor($nonCarProductListVisitor);
        /*** @var NonCarProductListResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter): Response
    {
        return new Response(200);
    }

    /**
     * 验车码
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        /*** @var $response ValidateCarResponse */
        $response = $this->execute(new ValidateCarRequest());
        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
}
